package com.xinya.wanandroidclient.ui.articleList

import androidx.lifecycle.MutableLiveData
import com.xinya.wanandroidclient.Constants
import com.xinya.wanandroidclient.base.BaseRepository
import com.xinya.wanandroidclient.bean.ArticleBean
import com.xinya.wanandroidclient.http.LoadState
import com.xinya.wanandroidclient.http.RetrofitManager
import com.xinya.wanandroidclient.http.WanAndroidApiService
import kotlinx.coroutines.CoroutineScope

/**
 *
 * @ClassName:      ArticleRepo$
 * @Description:    java类作用描述
 * @Author:         xinya
 * @CreateDate:     2021/3/2$ 18:44$
 * @UpdateRemark:   更新内容
 * @Version:        1.0
 */
class ArticleRepo(coroutineScope: CoroutineScope) : BaseRepository(coroutineScope) {
    fun getProjectArticle(
        type: Int,
        tabId: Int,
        currentPage: Int,
        articleLiveData: MutableLiveData<ArticleBean>,
        refreshStateLiveData: MutableLiveData<LoadState>
    ) {
        launch(
            block = {
                refreshStateLiveData.postValue(LoadState.Loading)
                if (type == Constants.PROJECT_TYPE) {
                    RetrofitManager.getApiService(WanAndroidApiService::class.java).getProjectArticle(currentPage, tabId)
                        .result()
                } else {
                    RetrofitManager.getApiService(WanAndroidApiService::class.java).getWXArticle(currentPage, tabId)
                        .result()
                }

            },
            success = {
                refreshStateLiveData.postValue(LoadState.Success)
                articleLiveData.postValue(it)
            },
            error = {
                refreshStateLiveData.postValue(LoadState.Error)
            }
        )
    }
}